
Imports cmplib01
Imports FMPLib01

Public Class frmTokenType
    Const c_MODULE As String = "frmTokenType"

    Private _cmp As CampaignerGameData
    Private _fmp As FlexMapData
    Private _cancelled As Boolean
    Private ptrToken As TokenType
    Private bEditMode As Boolean = True
    Private cMenu As New ContextMenu
    Private ruleMenu As New ContextMenu

    Public Shadows Sub ShowDialog(ByRef cmp As CampaignerGameData, ByRef fmp As FlexMapData)
        Try
            ptrToken = New TokenType
            ptrToken.FrontProperties = New TokenFlippableProperties(1, 100, 100, 5, 1, 0, 0, 0, 0, 0)
            ptrToken.FlippedProperties = New TokenFlippableProperties(1, 100, 100, 5, 1, 0, 0, 0, 0, 0)

            bEditMode = False

            ShowDialog(ptrToken, cmp, fmp)

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Public Shadows Sub ShowDialog(ByRef ptrTarget As TokenType, ByRef cmp As CampaignerGameData, ByRef fmp As FlexMapData)
        Try
            ptrToken = ptrTarget
            _cmp = cmp
            _fmp = fmp

            If bEditMode Then txtName.ReadOnly = True

            '// populate movement values (for the comboboxes)
            PopulateMovementBoxes()

            '// populate Lookup Text (los, size)
            PopulateValuesBoxes()

            '// then, show the properties of the token
            PopulateTokenProperties()

            '// indies
            ListIndicators()

            '// the menu for indicators, yay!
            CreateIndicatorMenu()

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try

        MyBase.ShowDialog()
    End Sub

    Public ReadOnly Property Token() As TokenType
        Get
            Return ptrToken
        End Get
    End Property

    Public ReadOnly Property Cancelled() As Boolean
        Get
            Return _cancelled
        End Get
    End Property

    Private Sub PopulateMovementBoxes()
        Try
            cboMovementTypeFront.Items.Clear()
            cboMovementTypeFlipped.Items.Clear()

            Dim i As Integer
            For i = 0 To _fmp.MoveTypeLookup.GetUpperBound(0) '.MoveClassLookup.GetUpperBound(0)
                cboMovementTypeFront.Items.Add(_fmp.MoveTypeLookup(i))
                cboMovementTypeFlipped.Items.Add(_fmp.MoveTypeLookup(i))
            Next

            cboMovementTypeFront.SelectedIndex = 0
            cboMovementTypeFlipped.SelectedIndex = 0

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Sub PopulateValuesBoxes()
        Try
            '// los power
            cboLOSpowerFront.Text = LOSPowerText(ptrToken.FrontProperties.LOSPower)
            cboLOSPowerFlipped.Text = LOSPowerText(ptrToken.FlippedProperties.LOSPower)

            '// size
            cboSizeFront.Text = SizeText(ptrToken.FrontProperties.Size)
            cboSizeFlipped.Text = SizeText(ptrToken.FlippedProperties.Size)

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Sub PopulateTokenProperties()
        Try
            '// front side
            numLOSRangeFront.Value = ptrToken.FrontProperties.LOSRange
            cboLOSpowerFront.Text = LOSPowerText(ptrToken.FrontProperties.LOSPower)
            cboSizeFront.Text = SizeText(ptrToken.FrontProperties.Size)
            numZOCRangeFront.Value = ptrToken.FrontProperties.ZOC
            numReinforcementRangeFront.Value = ptrToken.FrontProperties.ReinforcementRange
            numMovementSpeedFront.Value = ptrToken.FrontProperties.MovementSpeed
            cboMovementTypeFront.Text = _fmp.MoveTypeLookup(ptrToken.FrontProperties.MovementClassID)
            numTransportCostFront.Value = ptrToken.FrontProperties.TransportCost
            numTransportCapacityFront.Value = ptrToken.FrontProperties.TransportCapacity
            numPointValueFront.Value = ptrToken.FrontProperties.PointValue

            If Not ptrToken.FrontProperties.icon Is Nothing Then
                picIconFront.Image = ptrToken.FrontProperties.icon
            End If

            '// flipped side
            numLOSRangeflipped.Value = ptrToken.flippedProperties.LOSRange
            cboLOSpowerflipped.Text = LOSPowerText(ptrToken.flippedProperties.LOSPower)
            cboSizeflipped.Text = SizeText(ptrToken.flippedProperties.Size)
            numZOCRangeflipped.Value = ptrToken.flippedProperties.ZOC
            numReinforcementRangeflipped.Value = ptrToken.flippedProperties.ReinforcementRange
            numMovementSpeedflipped.Value = ptrToken.flippedProperties.MovementSpeed
            cboMovementTypeflipped.Text = _fmp.MoveTypeLookup(ptrToken.flippedProperties.MovementClassID)
            numTransportCostflipped.Value = ptrToken.flippedProperties.TransportCost
            numTransportCapacityflipped.Value = ptrToken.FlippedProperties.TransportCapacity
            numPointValueFlipped.Value = ptrToken.FlippedProperties.PointValue

            If Not ptrToken.FlippedProperties.icon Is Nothing Then
                picIconFlipped.Image = ptrToken.FlippedProperties.icon
            End If

            ptrToken.tokenTypeName = txtName.Text

            '// token rules
            lstTokenRules.Items.Clear()
            For Each obj As RuleType In ptrToken.TokenRules
                lstTokenRules.Items.Add(obj)
            Next

            'ptrToken.TokenRules.Clear()
            'For Each obj As RuleType In lstTokenRules.Items
            '    ptrToken.TokenRules.Add(obj)
            'Next

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub


    Private Sub SetTokenProperties()
        '// this will set the properties for the token type. Yay!
        Try
            ptrToken.FrontProperties = New TokenFlippableProperties(numLOSRangeFront.Value, SizeValue(cboSizeFront.Text), LOSPowerValue(cboLOSpowerFront.Text), numMovementSpeedFront.Value, cboMovementTypeFront.SelectedIndex, numPointValueFront.Value, numZOCRangeFront.Value, numReinforcementRangeFront.Value, numTransportCostFront.Value, numTransportCapacityFront.Value, picIconFront.Image)
            ptrToken.FlippedProperties = New TokenFlippableProperties(numLOSRangeFlipped.Value, SizeValue(cboSizeFlipped.Text), LOSPowerValue(cboLOSPowerFlipped.Text), numMovementSpeedFlipped.Value, cboMovementTypeFlipped.SelectedIndex, numPointValueFlipped.Value, numZOCRangeFlipped.Value, numReinforcementRangeFlipped.Value, numTransportCostFlipped.Value, numTransportCapacityFlipped.Value, picIconFlipped.Image)
            ptrToken.tokenTypeName = txtName.Text

            '// rules
            ptrToken.TokenRules.Clear()
            For Each obj As RuleType In lstTokenRules.Items
                ptrToken.TokenRules.Add(obj)
            Next

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Function SizeText(ByVal size As Single) As String
        Select Case size
            Case Is <= 5 : Return "Tiny"
            Case Is <= 10 : Return "Very Small"
            Case Is <= 25 : Return "Slight"
            Case Is <= 50 : Return "Small"
            Case Is <= 100 : Return "Average"
            Case Is <= 200 : Return "Big"
            Case Is <= 400 : Return "Large"
            Case Is <= 600 : Return "Immense"
            Case Is <= 1000 : Return "Huge"
            Case Is <= 1500 : Return "Massive"
            Case Is <= 10000 : Return "Titanic"
            Case Else : Return "Average"
        End Select
    End Function

    Private Function SizeValue(ByVal size As String) As Single
        Select Case size
            Case "Tiny" : Return 5
            Case "Very Small" : Return 10
            Case "Slight" : Return 25
            Case "Small" : Return 50
            Case "Average" : Return 100
            Case "Big" : Return 200
            Case "Large" : Return 400
            Case "Immense" : Return 600
            Case "Huge" : Return 1000
            Case "Massive" : Return 1500
            Case "Titanic" : Return 10000
            Case Else : Return "Average"
        End Select
    End Function

    Private Function LOSPowerText(ByVal power As Single) As String
        Select Case power
            Case Is >= 150 : Return "Weak"
            Case Is >= 120 : Return "Low"
            Case Is >= 100 : Return "Average"
            Case Is >= 80 : Return "Good"
            Case Is >= 60 : Return "Strong"
            Case Is >= 50 : Return "Powerful"
            Case Is >= 30 : Return "Very Powerful"
            Case Else
                Return "Average"
        End Select
    End Function

    Private Function LOSPowerValue(ByVal power As String) As Single
        Select Case power
            Case "Weak" : Return 150
            Case "Low" : Return 120
            Case "Average" : Return 100
            Case "Good" : Return 80
            Case "Strong" : Return 60
            Case "Powerful" : Return 50
            Case "Very Powerful" : Return 30
            Case Else
                Return 1
        End Select
    End Function

    Private Sub LoadIcon(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picIconFront.Click, picIconFlipped.Click
        Dim path As String
        Dim fDiag As New OpenFileDialog()

        fDiag.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF; *.PNG"

        fDiag.ShowDialog()
        path = fDiag.FileName
        If path = "" Then Exit Sub
        Try
            DirectCast(sender, PictureBox).Image = Bitmap.FromFile(path)
        Catch ex As Exception
            MsgBox("Could not load image. Please check to make sure you have a valid image file.", MsgBoxStyle.Critical, "Could Not Load Image")
        End Try

    End Sub

    Private Sub btnAddRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRule.Click
        Try
            Dim fSelect As New frmAssignRules(ptrToken, _cmp)
            fSelect.ShowDialog()
            ListExistingRules()

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Sub ListExistingRules()
        Try
            lstTokenRules.Items.Clear()
            For Each rult As cmplib01.RuleType In ptrToken.TokenRules
                lstTokenRules.Items.Add(rult)
            Next
        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Sub btnRemoveRule_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemoveRule.Click
        Try
            If lstTokenRules.SelectedItem Is Nothing Then Exit Sub
            ptrToken.TokenRules.Remove(lstTokenRules.SelectedItem)
            lstTokenRules.Items.Remove(lstTokenRules.SelectedItem)
        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _cancelled = True
        Me.Hide()
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try

            If Not bEditMode Then
                If _cmp.TokenTypes.ContainsKey(ptrToken.tokenTypeName) Then
                    MessageBox.Show("A token type with that name already exists. Please change the token type name.", "Duplicate Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Sub
                End If
            End If

            _cancelled = False
            SetTokenProperties()
            Me.Hide()
        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub

    Private Sub btnAddIndicator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddIndicator.Click
        cMenu.Show(sender, New System.Drawing.Point(0, 0))
    End Sub


    Private Sub CreateRuleMenu()
        Try
            ''/// hmmn, I guess we can make this similar later
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CreateIndicatorMenu()
        Try
            Dim key As String
            For Each key In _cmp.Indicators.Keys
                Dim indi As cmplib01.IIindciator = _cmp.Indicators.Item(key)
                Dim menu As New MenuItem(indi.IndicatorName, AddressOf IndicatorClick)

                menu.Tag = indi
                cMenu.MenuItems.Add(menu)
            Next

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub IndicatorClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            '// add the indicator to the chit

            Try
                Dim indi As cmplib01.IIindciator = sender.tag
                If ptrToken.Indicators.ContainsKey(indi.IndicatorName) Then
                    MessageBox.Show("An indicator with that name already exists in this chit.", "Invalid Indicator", MessageBoxButtons.OK, MessageBoxIcon.Hand)
                    Exit Sub
                End If
                ptrToken.Indicators.Add(indi.IndicatorName, indi)
                ListIndicators()
            Catch ex As Exception

            End Try

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub ListIndicators()
        Try

            If ptrToken.Indicators Is Nothing Then ptrToken.Indicators = New Hashtable

            lstIndicators.Items.Clear()
            Dim k As String
            For Each k In ptrToken.Indicators.Keys
                Dim indi As cmplib01.IIindciator = ptrToken.Indicators.Item(k)
                lstIndicators.Items.Add(indi)
            Next

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub btnRemoveIndicator_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveIndicator.Click
        If lstIndicators.SelectedItem Is Nothing Then Exit Sub
        Try
            Dim indi As cmplib01.IIindciator
            indi = lstIndicators.SelectedItem
            ptrToken.Indicators.Remove(indi.IndicatorName)

            ListIndicators()

        Catch ex As Exception
            HandleException(c_MODULE, ex)
        End Try
    End Sub
End Class